package com.joolun.web.controller.promotion;

import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.core.page.TableDataInfo;
import com.joolun.common.enums.BusinessType;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.system.domain.EduSchedulingClass;
import com.joolun.system.service.IEduSchedulingService;
import com.joolun.weixin.service.IEduSchedulingClassService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 排期班级派量Controller
 * 
 * @author joolun
 * @date 2025-03-05
 */
@RestController
@Slf4j
@RequestMapping("/promotion/class")
public class EduSchedulingClassController extends BaseController
{
    @Autowired
    private IEduSchedulingClassService eduSchedulingClassService;

    /**
     * 查询排期班级派量列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EduSchedulingClass eduSchedulingClass)
    {
        startPage();
        List<EduSchedulingClass> list = eduSchedulingClassService.selectEduSchedulingClassList(eduSchedulingClass);
        return getDataTable(list);
    }
    /**
     * 查询排期班级派量列表
     */
    @GetMapping("/listno")
    public TableDataInfo listno(EduSchedulingClass eduSchedulingClass)
    {
        List<EduSchedulingClass> list = eduSchedulingClassService.selectEduSchedulingClassList(eduSchedulingClass);
        return getDataTable(list);
    }
    /**
     * 导出排期班级派量列表
     */
    @PreAuthorize("@ss.hasPermi('promotion:class:export') || @ss.hasPermi('advanced:class:export')")
    @Log(title = "排期班级派量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduSchedulingClass eduSchedulingClass)
    {
        List<EduSchedulingClass> list = eduSchedulingClassService.selectEduSchedulingClassList(eduSchedulingClass);
        ExcelUtil<EduSchedulingClass> util = new ExcelUtil<EduSchedulingClass>(EduSchedulingClass.class);
        util.exportExcel(response, list, "排期班级派量数据");
    }

    /**
     * 获取排期班级派量详细信息
     */
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable("classId") Long classId)
    {
        return success(eduSchedulingClassService.selectEduSchedulingClassByClassId(classId));
    }

    /**
     * 新增排期班级派量
     */
    @PreAuthorize("@ss.hasPermi('promotion:class:add') || @ss.hasPermi('advanced:class:add')")
    @Log(title = "排期班级派量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduSchedulingClass eduSchedulingClass)
    {
        eduSchedulingClass.setCreateBy(getUsername());
        return toAjax(eduSchedulingClassService.insertEduSchedulingClass(eduSchedulingClass));
    }

    /**
     * 修改排期班级派量
     */
    @PreAuthorize("@ss.hasPermi('promotion:class:edit') || @ss.hasPermi('advanced:class:edit')")
    @Log(title = "排期班级派量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduSchedulingClass eduSchedulingClass)
    {
        eduSchedulingClass.setCreateBy(getUsername());
        return toAjax(eduSchedulingClassService.updateEduSchedulingClass(eduSchedulingClass));
    }

    /**
     * 删除排期班级派量
     */
    @PreAuthorize("@ss.hasPermi('promotion:class:remove') || @ss.hasPermi('advanced:class:remove')")
    @Log(title = "排期班级派量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{classIds}")
    public AjaxResult remove(@PathVariable Long[] classIds)
    {
        return toAjax(eduSchedulingClassService.deleteEduSchedulingClassByClassIds(classIds));
    }

    /**
     * 查询排期班级派量列表
     */
    @GetMapping("/count")
    public AjaxResult count(Long schedulingClassId)
    {
        EduSchedulingClass eduSchedulingClass = new EduSchedulingClass();
        eduSchedulingClass.setSchedulingId(schedulingClassId);
        List<EduSchedulingClass> list = eduSchedulingClassService.selectEduSchedulingClassList(eduSchedulingClass);
        Map<String,Object> map = new HashMap<>();
        map.put("all",list.size());
        map.put("exist",list.stream().filter(item->item.getReceivedQuantityStatus()==1).collect(Collectors.toList()).size());
        map.put("stop",list.stream().filter(item->item.getReceivedQuantityStatus()==2).collect(Collectors.toList()).size());
        return success(map);
    }


    /**
     * 获取有效的排期联系人的获客统计数据
     */
    @GetMapping("/getUserBehaviorData")
    public void getUserBehaviorData(){
        eduSchedulingClassService.getUserBehaviorData();
    }


}
